using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework;
using Sharpie.Libs;
using SharpieAndroid.Libs;
using System.IO;
using Sharpie;

namespace SharpieAndroid.States
{
	public class MapSelect : DrawableGameComponent
	{
		SpriteBatch _batch;
		SpriteFont FontNormal, FontBigB, FontNormalB;
		List<MapButton> mapbuttons;
		int columns;
		int spaceWidth, buttonWidth, buttonHeight;
		Rectangle MenuMargin;
		ButtonMenu bottomMenu;

		private string mapsPath = Path.Combine(Activity1.AppDataPath, "Maps");

		public MapSelect(Game game)
			: base(game) { }

		public override void Initialize()
		{
			base.Initialize();
			MenuMargin = new Rectangle(25, 25, GraphicsDevice.Viewport.Width - 25, GraphicsDevice.Viewport.Height - 90);
			buttonWidth = 200;
			columns = 0;
			do
			{
				columns++;
			} while (MenuMargin.Width > buttonWidth * (columns + 1) + (10 * columns));
			spaceWidth = (MenuMargin.Width - (buttonWidth * columns)) / columns;
			buttonHeight = (int)((MenuMargin.Height - spaceWidth) / 2);
			LoadMaps();
			bottomMenu = new ButtonMenu(Point.Zero, Game);
			bottomMenu.AddButton(LabelButton.Type.NORMAL, "{BACK}", Color.IndianRed, Color.White);
			bottomMenu.Location = new Point(GraphicsDevice.Viewport.Width, GraphicsDevice.Viewport.Height);
		}

		protected override void LoadContent()
		{
			base.LoadContent();
			_batch = new SpriteBatch(GraphicsDevice);
			FontNormal = Game.Content.Load<SpriteFont>("Fonts\\16pt");
			FontNormalB = Game.Content.Load<SpriteFont>("Fonts\\16ptB");
			FontBigB = Game.Content.Load<SpriteFont>("Fonts\\20ptB");
		}

		public override void Update(GameTime gameTime)
		{
			base.Update(gameTime);
			bool UpRow = false;
			for (int i = 0; i < mapbuttons.Count; i++)
			{
				if (i % 3 == 0)
				{
					if (UpRow) UpRow = false;
					else UpRow = true;
				}
				if (UpRow)
				{
					mapbuttons[i].Rectangle = new Rectangle(10 + buttonWidth * i + spaceWidth * i, MenuMargin.Y, buttonWidth, buttonHeight);
				}
				else
				{
					mapbuttons[i].Rectangle = new Rectangle(10 + buttonWidth * i + spaceWidth * i, MenuMargin.Y + buttonHeight + spaceWidth, buttonWidth, buttonHeight);
				}
				mapbuttons[i].Update(gameTime);
			}

			bottomMenu.Update(gameTime);
			if (bottomMenu.ClickedPos == 0) StateManager.PreviousState(Screen.HideStyle.Fade);
		}

		public override void Draw(GameTime gameTime)
		{
			base.Draw(gameTime);
			foreach (MapButton mb in mapbuttons)
			{
				mb.Draw(gameTime);
			}
			bottomMenu.Draw(gameTime);
		}

		public void LoadMaps()
		{
			List<Map> maplist = new List<Map>();
			for (int i = 1; i <= 4; i++) // default maps loading
			{
				try
				{
					maplist.Add(new Map(Game.Activity.Assets.Open(@"Maps/Map" + i + ".smf")));
				}
				catch (Exception) { }
			}

			foreach (string s in Directory.GetFiles(mapsPath))
			{
				if (Path.GetExtension(Path.Combine(mapsPath, s)) == ".smf")
				{
					maplist.Add(new Map(File.Open(s, FileMode.Open)));
				}
			}
			mapbuttons = new List<MapButton>();
			foreach (Map m in maplist)
			{
				mapbuttons.Add(new MapButton(m, Game));
			}
		}
	}
}